package com.zyh.server.controller;

import com.zyh.server.config.OSSService;
import com.zyh.server.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: zyh
 * @Date: 2024-11-14-15:47
 * @Description
 */
@Tag(name="文件上传模块")
@Slf4j
@RestController //@Controller + @ResponseBody后端传递json数据
@RequestMapping("/upload")
@CrossOrigin(origins = "*") //允许跨域请求
public class UploadController {
	@Autowired
	private OSSService ossService;

	@Operation(summary = "图片上传")
	@Parameters({
			@Parameter(name = "type",description = "图片类型",in= ParameterIn.QUERY),
			@Parameter(name = "file",description = "文件",in= ParameterIn.QUERY)
	})
	@PostMapping("/picture")
	public R uploadFile(
			@RequestParam("type") String type,
			@RequestParam("file") MultipartFile file) {
		try {
			log.info("文件类型：{},文件名：{}",type,file.getOriginalFilename());
			String aliyunUrl = ossService.uploadFile(type, file);
			// 将地址返回给前端
			return R.ok().setData(aliyunUrl);
		} catch (Exception e) {
			e.printStackTrace();
			return R.fail().setMsg("文件上传失败！");
		}

	}

}
